# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

# Create your views here.
# _*_ coding:utf-8 _*_
import json
from django.shortcuts import render

from django.views.generic.base import View

from random import randint
from .models import Article
from message.models import Mseeage
from django.http import HttpResponse, HttpResponseRedirect  #HttpResponseRedirect重定View

from django.core.urlresolvers import reverse


# 通过类实现邮箱登陆


class IndexView(View):
    def get(self, request):
        articles = Article.objects.all().order_by('click_nums')[:6]


        articlerand=Article.objects.all().order_by('?')[:3] #会生成排序表 数据大会非常耗时
        # last = MyModel.objects.count() - 1
        # # 这是一个获取两个不重复随机数的简单方法
        # index1 = randint(0, last)
        # index2 = randint(0, last - 1)
        # if index2 == index1:
        #     index2 = last
        # MyObj1 = MyModel.objects.all()[index1]
        # MyObj2 = MyModel.objects.all()[index2]
        flag = 'index'
        return render(request, 'index.html', {
                                                'articles': articles,
                                                'flag' : flag,
                                                'articlerand':articlerand

        })


class TimeFileView(View):
    def get(self, request):
        articles = Article.objects.all().order_by('-id')[:10]
        articlelast=Article.objects.all().order_by('-id')[:1]


        articlerand = Article.objects.all().order_by('?')[:3]
        flag = 'time'

        articless = []
        if articles:
            for article in articles:
                articlex = {
                    "id": article.id,
                    "add_time":article.add_time.strftime('%m-%d'),

                    "title" : article.title,
                    "click_nums ":article.click_nums,

                }
                articless.append(articlex)


        return render(request, 'time_file.htm', {
                                                'articles': articless,
                                                'flag' : flag,
                                                'articlerand':articlerand,
                                                'articlelast':articlelast,


        })

class SuiBiView(View):
    def get(self, request):
        articles = Article.objects.filter(article_type=2).order_by('click_nums')[:6]

        articlerand = Article.objects.all().order_by('?')[:3]  # 会生成排序表 数据大会非常耗时

        flag = 'suibi'
        return render(request, 'index.html', {
            'articles': articles,
            'flag': flag,
            'articlerand': articlerand

        })



class ArticleView(View):
    def get(self, request):
        id= int(request.GET.get("id", "-1"))
        article = Article.objects.filter(id=id)
        articlelast = article
        articlerand = Article.objects.all().order_by('?')[:3]  # 会生成排序表 数据大会非常耗时

        lastarticle= Article.objects.filter(pk__gt=id).order_by('id')[:1]
        prearticle = Article.objects.filter(pk__lt=id).order_by('id')[:1]

        if article:
            flag = 'article'
            return render(request, 'article.html', {
                'article': article.first,
                'flag': flag,
                'articlerand': articlerand,
                'articlelast': articlelast,

                'prearticle':prearticle.last,
                'lastarticle':lastarticle.first,


            })


        else:
            return HttpResponseRedirect(reverse('index'))



